package com.hsp.api;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Author Stringzhua
 * @Date 2024/3/26 19:25
 * description:INetAddress 类的使用
 */
public class Api_ {
    public static void main(String[] args) throws UnknownHostException {
        //1.获取本机的InetAddress对象
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost);//LAPTOP-6Q5PO9I/192.168.12.1

        //2.根据指定的主机名获取InetAddress对象
        InetAddress host1 = InetAddress.getByName("LAPTOP-6Q5PO9I");
        System.out.println(host1);//LAPTOP-6Q5PO9I/192.168.12.1 可以拿到该主机名识别到的ip

        //3.根据域名返回 InetAddress对象,比如获取百度[www.baidu.com]的对应的InetAddress对象
        InetAddress host2 = InetAddress.getByName("www.baidu.com");
        System.out.println(host2);//www.baidu.com/36.155.132.76

        InetAddress host3 = InetAddress.getByName("www.github.com");
        System.out.println(host3);//www.github.com/20.205.243.166

        //4.通过InetAddress对象，获取对应的地址
        String hostAddress = host2.getHostAddress();//IP地址
        System.out.println("host2对应的ip=" + hostAddress);//host2对应的ip=36.155.132.76

        //5.通过InetAddress对象,获取对应的主机名/域名
        String hostName = host2.getHostName();
        System.out.println("host2对应的主机名/域名为=" + hostName);//host2对应的主机名/域名为=www.baidu.com
    }
}
